package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.localfun.info.SupplierSystemOrderLocalFunDetailInfoDTO;
import com.ctshk.rpc.booking.dto.system.localfun.info.SupplierSystemOrderLocalFunInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderLocalFun;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderLocalFunDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderVisaDetail;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderLocalFunDetailMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderLocalFunMapper;
import com.ctshk.rpc.booking.req.system.localfun.add.SupplierSystemOrderLocalFunAddReq;
import com.ctshk.rpc.booking.req.system.localfun.add.SupplierSystemOrderLocalFunDetailAddReq;
import com.ctshk.rpc.booking.req.system.localfun.edit.SupplierSystemOrderLocalFunEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderLocalFunService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统内当地玩乐基础信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-25
 */
@DubboService
public class SupplierSystemOrderLocalFunServiceImpl implements ISupplierSystemOrderLocalFunService {
    @Autowired
    SupplierSystemOrderLocalFunMapper supplierSystemOrderLocalFunMapper;
    @Autowired
    SupplierSystemOrderLocalFunDetailMapper supplierSystemOrderLocalFunDetailMapper;

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderLocalFunAddReq> supplierSystemOrderLocalFunAddReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderLocalFunAddReqList)) {
            List<SupplierSystemOrderLocalFun> supplierSystemOrderLocalFunList = new ArrayList<>();
            supplierSystemOrderLocalFunAddReqList.forEach(localFunAddReq -> {
                SupplierSystemOrderLocalFun supplierSystemOrderLocalFun = EntityUtil.copy(localFunAddReq, SupplierSystemOrderLocalFun.class);
                supplierSystemOrderLocalFun.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderLocalFun.setOrderId(id);
                supplierSystemOrderLocalFun.setCreateId(userId);
                supplierSystemOrderLocalFun.setGmtCreate(nowDateTime);
                supplierSystemOrderLocalFun.setModifiedId(userId);
                supplierSystemOrderLocalFun.setGmtModified(nowDateTime);
                supplierSystemOrderLocalFun.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderLocalFunList.add(supplierSystemOrderLocalFun);

                //添加旅客
                List<SupplierSystemOrderLocalFunDetailAddReq> supplierSystemOrderLocalFunDetailAddReqList = localFunAddReq.getSupplierSystemOrderLocalFunDetailAddReqList();
                if (CollectionUtils.isNotEmpty(supplierSystemOrderLocalFunDetailAddReqList)) {
                    List<SupplierSystemOrderLocalFunDetail> supplierSystemOrderLocalFunDetails = new ArrayList<>();
                    supplierSystemOrderLocalFunDetailAddReqList.forEach(localFunTouristAdd -> {
                        SupplierSystemOrderLocalFunDetail supplierSystemOrderLocalFunDetail = EntityUtil.copy(localFunTouristAdd, SupplierSystemOrderLocalFunDetail.class);
                        supplierSystemOrderLocalFunDetail.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderLocalFunDetail.setOrderId(id);
                        supplierSystemOrderLocalFunDetail.setAirExpressInfoId(supplierSystemOrderLocalFun.getId());
                        supplierSystemOrderLocalFunDetail.setCreateId(userId);
                        supplierSystemOrderLocalFunDetail.setGmtCreate(nowDateTime);
                        supplierSystemOrderLocalFunDetail.setModifiedId(userId);
                        supplierSystemOrderLocalFunDetail.setGmtModified(nowDateTime);
                        supplierSystemOrderLocalFunDetail.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderLocalFunDetails.add(supplierSystemOrderLocalFunDetail);
                    });
                    supplierSystemOrderLocalFunDetailMapper.insertBatchSomeColumn(supplierSystemOrderLocalFunDetails);
                }
            });
            supplierSystemOrderLocalFunMapper.insertBatchSomeColumn(supplierSystemOrderLocalFunList);
        }
    }

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderLocalFunEditReq> supplierSystemOrderLocalFunEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderLocalFunEditReqList)) {
            supplierSystemOrderLocalFunEditReqList.forEach(localFunEditReq -> {
                UpdateWrapper<SupplierSystemOrderLocalFun> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderLocalFun::getId, localFunEditReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderLocalFun::getCustomerRemark, localFunEditReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderLocalFun::getSupplierRemark, localFunEditReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderLocalFun::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderLocalFun::getGmtModified, nowDateTime);
                supplierSystemOrderLocalFunMapper.update(new SupplierSystemOrderLocalFun(), updateWrapper);
                localFunEditReq.getSupplierSystemOrderLocalFunDetailEditReqList().forEach(localFunDetailReq -> {
                    UpdateWrapper<SupplierSystemOrderLocalFunDetail> updateDetailWrapper = new UpdateWrapper<>();
                    updateDetailWrapper.lambda().eq(SupplierSystemOrderLocalFunDetail::getId, localFunDetailReq.getId());
                    updateDetailWrapper.lambda().set(SupplierSystemOrderLocalFunDetail::getPrice, localFunDetailReq.getPrice());
                    updateDetailWrapper.lambda().set(SupplierSystemOrderLocalFunDetail::getModifiedId, userId);
                    updateDetailWrapper.lambda().set(SupplierSystemOrderLocalFunDetail::getGmtModified, nowDateTime);
                    supplierSystemOrderLocalFunDetailMapper.update(null, updateDetailWrapper);
                });
            });
        }
    }

    @Override
    public List<SupplierSystemOrderLocalFunInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderLocalFun> localFunQueryWrapper = new QueryWrapper<>();
        localFunQueryWrapper.lambda().eq(SupplierSystemOrderLocalFun::getOrderId, id);
        List<SupplierSystemOrderLocalFun> supplierSystemOrderLocalFunList = supplierSystemOrderLocalFunMapper.selectList(localFunQueryWrapper);
        List<SupplierSystemOrderLocalFunInfoDTO> supplierSystemOrderLocalFunInfoDTOList = EntityUtil.copyList(supplierSystemOrderLocalFunList, SupplierSystemOrderLocalFunInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderLocalFunInfoDTOList)) {
            supplierSystemOrderLocalFunInfoDTOList.forEach(localFunCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderLocalFunDetail> localFunTouristQueryWrapper = new QueryWrapper<>();
                localFunTouristQueryWrapper.lambda().eq(SupplierSystemOrderLocalFunDetail::getOrderId, id);
                localFunTouristQueryWrapper.lambda().eq(SupplierSystemOrderLocalFunDetail::getAirExpressInfoId, localFunCheckinDTO.getId());
                List<SupplierSystemOrderLocalFunDetail> supplierSystemOrderLocalFunDetails = supplierSystemOrderLocalFunDetailMapper.selectList(localFunTouristQueryWrapper);
                List<SupplierSystemOrderLocalFunDetailInfoDTO> supplierSystemOrderLocalFunDetailInfoDTOS = EntityUtil.copyList(supplierSystemOrderLocalFunDetails, SupplierSystemOrderLocalFunDetailInfoDTO.class);
                localFunCheckinDTO.setSupplierSystemOrderLocalFunDetailInfoDTOList(supplierSystemOrderLocalFunDetailInfoDTOS);
            });
        }
        return supplierSystemOrderLocalFunInfoDTOList;
    }
}
